package com.woniu.common.utils;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson2.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.woniu.common.dto.ZyManageDto;
import com.woniu.common.entity.ShortMes;
import java.io.IOException;


public class SMSUtils {

    public static void send(ShortMes shortMes) throws IOException {

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tKs5w6w9iQMVYeL8Xo1", "rYxBFHPrKcCNXdz1FfAlIQSHyjY0wF");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", shortMes.getAccount());//往什么手机号发
        request.putQueryParameter("SignName", "MT4账号");
        request.putQueryParameter("TemplateCode", "SMS_157449478");
        request.putQueryParameter("TemplateParam", JSON.toJSONString(shortMes));//发送的内容,
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
    public static String getCode(ZyManageDto zyManageDto) throws IOException {
        String code = RandomUtil.randomNumbers(4);
        ShortMes shortMes = new ShortMes();
        shortMes.setPassword(code);
        System.out.println(zyManageDto.getManagePhone());
        shortMes.setAccount(zyManageDto.getManagePhone());
        shortMes.setName("用户");
        send(shortMes);


//        String code = RandomUtil.randomNumbers(4);
//        ShortMes shortMes = new ShortMes();
//        shortMes.setPassword(code);
//        shortMes.setAccount(user.getMobile());
//        shortMes.setName(user.getNickname());
//        send(shortMes);
        return code;
    }
}
